package com.atguigu.dga.governance.assessor.quality;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.ds.bean.TDsTaskInstance;
import com.atguigu.dga.ds.service.TDsTaskInstanceService;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 表产出时效监控指标
 * 当日产出时效，超过前{days}天产出时效平均值{percent}% 则给0分，其余10分
 * @Author: lay
 * @Date: 2024/6/30 16:28
 */
@Component("TABLE_PRODUCE_TIMELINESS")
public class TableProduceTimelinessAssessor extends Assessor {
    
    @Autowired
    private TDsTaskInstanceService tDsTaskInstanceService;

    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //ods层的表没有产出直接过滤掉
        if (assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel().equals(DgaConstant.DW_LEVEL_ODS)){
            return;
        }
        //获取参数
        String paramsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(paramsJson);
        Integer days = jsonObject.getInteger("days");
        Integer percent = jsonObject.getInteger("percent");

        TDsTaskInstance tDsTaskInstance = assessParam.getTDsTaskInstance();
        Date submitTime = tDsTaskInstance.getSubmitTime();
        Date endTime = tDsTaskInstance.getEndTime();

        //当日产出时效
        Long currentProvideMs = endTime.getTime() - submitTime.getTime();
        
        //前days天的任务实例
        String assessDate = assessParam.getAssessDate();
        Date currentDate = DateUtils.parseDate(assessDate, "yyyy-MM-dd");
        Date date = DateUtils.addDays(currentDate, -days);
        //开始时间
        String startDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        List<TDsTaskInstance> beforeDaystDsTaskInstanceList = tDsTaskInstanceService.getBeforeDaystDsTaskInstanceList(
                assessParam.getTableMetaInfo().getSchemaName() + "." + assessParam.getTableMetaInfo().getTableName(),
                startDate,
                assessDate
        );

        Long totalProvideMs = 0L;
        for (TDsTaskInstance beforetDsTaskInstance : beforeDaystDsTaskInstanceList) {
            Long beforeProvideMs = beforetDsTaskInstance.getEndTime().getTime() - beforetDsTaskInstance.getSubmitTime().getTime();
            totalProvideMs += beforeProvideMs;
        }

        int realDays = beforeDaystDsTaskInstanceList.size();
        if (realDays > 0){
            Long avgProvideMs = totalProvideMs / realDays;
            if (currentProvideMs > avgProvideMs && (currentProvideMs - avgProvideMs) * 100 / avgProvideMs > percent){
                //给分
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                //问题项
                governanceAssessDetail.setAssessProblem("当日产出时效超过前" + days + "天平均产出时效的" + percent + "%");
                //考评备注
                governanceAssessDetail.setAssessComment("当日的时效: " + currentProvideMs + " , 前 " + realDays + "天的平均产出时效为: " + avgProvideMs);
            }
        }
    }
}
